//
//  BankCardViewController.m
//  YDD
//
//  Created by 欧洪飞 on 2017/8/22.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "BankCardViewController.h"

#import "BankCardCell.h"
#import "AddCardViewController.h"
@interface BankCardViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSMutableArray *array1;//数据源
}

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UIView *noView;


@end

@implementation BankCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [self showNav:@"选择银行卡"];
    array1 = [NSMutableArray array];
    
    [self loadData];
    [self loadUI];
    
}

- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, 64) style:UITableViewStylePlain];
    _tableView.dataSource = self;//44*10+60
    _tableView.delegate = self;
    _tableView.scrollEnabled = NO;
    _tableView.rowHeight = 65.0;
    [self.view addSubview:_tableView];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, ScreenHeight-50, ScreenWidth, 50.0);
    btn.backgroundColor = [UIColor whiteColor];
    [btn setTitle:@" 添加银行卡" forState:UIControlStateNormal];
    [btn setTitleColor:COLOR_RGB(65, 132, 255, 1) forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"矩形-21-拷贝"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clickAddCard) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    _noView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64-50)];
    _noView.hidden = YES;
    [self.view addSubview:_noView];
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(90, 110, 200, 20)];
    lab.text = @"一张银行卡也没有，请添加^_^";
    [_noView addSubview:lab];
    
}

- (void)clickAddCard{
    AddCardViewController *addcard = [[AddCardViewController alloc] init];
    [self.navigationController pushViewController:addcard animated:YES];
}

#pragma -mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return array1.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell";
    BankCardCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"BankCardCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *dic = array1[indexPath.row];
    
    cell.labTit.text = [dic objectForKey:@"bankname"];
    cell.labDet.text = [dic objectForKey:@"banknumber"];
    NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],[dic objectForKey:@"banklogo"]];
    [cell.images sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"工行"]];
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //设置默认银行卡
    //[self loadData2];
    //删除
    //[self loadData3];
    
}

//展示银行卡
- (void)loadData{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSString *str = [NSString stringWithFormat:@"%@890d69e502d1d37a9e84335ccefece57",[user objectForKey:@"token2"]];
    NSString *token = [LCMD5Tool md5:str];
    
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:token forKey:@"verifyToken"];
    [parmas setObject:[user objectForKey:@"userid"] forKey:@"userid"];
    
    NSString *url = [NSString stringWithFormat:@"%@/App/Usercenter/banklist",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"展示银行卡res:%@",responseObject);
        NSString *state = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"status"] integerValue]];
        if ([state integerValue] == 200) {
            array1 = [responseObject objectForKey:@"content"];
            
        }else if ([state integerValue] == 205){//没有银行卡
            _tableView.hidden = YES;
            _noView.hidden = NO;
        }
        
        [_tableView reloadData];
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
    
}
//设置默认银行卡
- (void)loadData2{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSString *str = [NSString stringWithFormat:@"%@890d69e502d1d37a9e84335ccefece57",[user objectForKey:@"token2"]];
    NSString *token = [LCMD5Tool md5:str];
    
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:token forKey:@"verifyToken"];
    [parmas setObject:[user objectForKey:@"userid"] forKey:@"userid"];
    [parmas setObject:@"4" forKey:@"bankid"];
    
    NSString *url = [NSString stringWithFormat:@"%@/App/Usercenter/settingbank",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"设置默认银行卡res:%@",responseObject);
        
        [_tableView reloadData];
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
}
//删除银行卡
- (void)loadData3{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSString *str = [NSString stringWithFormat:@"%@890d69e502d1d37a9e84335ccefece57",[user objectForKey:@"token2"]];
    NSString *token = [LCMD5Tool md5:str];
    
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:token forKey:@"verifyToken"];
    [parmas setObject:[user objectForKey:@"userid"] forKey:@"userid"];
    [parmas setObject:@"4" forKey:@"bankid"];
    
    NSString *url = [NSString stringWithFormat:@"%@/App/Usercenter/deleBank",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"删除银行卡res:%@",responseObject);
        NSString *state = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"status"] integerValue]];
        NSString *msg = [responseObject objectForKey:@"msg"];
        if ([state integerValue] == 200) {
            [self showAlert:@"删除成功"];
        }else{
            [SVProgressHUD showErrorWithStatus:msg];
        }
        
        [_tableView reloadData];
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
